package com.zdpx.pxframework.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdpx.pxframework.core.pojo.LoginUser;
import com.zdpx.pxframework.core.validator.group.AddGroup;
import com.zdpx.pxframework.core.validator.group.UpdateGroup;
import com.zdpx.pxframework.core.vo.PageResult;
import com.zdpx.pxframework.core.vo.Result;
import com.zdpx.pxframework.security.annotation.RequiresLogin;
import com.zdpx.pxframework.system.api.MenuInfoControllerApi;
import com.zdpx.pxframework.system.entity.MenuInfo;
import com.zdpx.pxframework.system.service.MenuInfoService;
import com.zdpx.pxframework.system.vo.query.MenuInfoQuery;
import com.zdpx.pxframework.system.vo.vo.MenuInfoTreeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


/**
 * 菜单 接口控制器
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022-02-27 15:20:14
 */
@RestController
@RequestMapping("/menu-info")
public class MenuInfoController implements MenuInfoControllerApi {
    @Resource
    private MenuInfoService menuInfoService;


    /**
     * 获取条件查询信息
     *
     * @param menuInfoQuery 查询条件
     * @return 查询封装
     */
    private LambdaQueryWrapper<MenuInfo> getMenuInfoQueryWrapper(MenuInfoQuery menuInfoQuery) {
        LambdaQueryWrapper<MenuInfo> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(menuInfoQuery.getMenuKey())) {
            wrapper.like(MenuInfo::getMenuKey, menuInfoQuery.getMenuKey());
        }
        if (!StringUtils.isEmpty(menuInfoQuery.getMenuName())) {
            wrapper.like(MenuInfo::getMenuName, menuInfoQuery.getMenuName());
        }
        if (menuInfoQuery.getType() != null) {
            wrapper.eq(MenuInfo::getType, menuInfoQuery.getType());
        }
        if (menuInfoQuery.getLevel() != null) {
            wrapper.eq(MenuInfo::getLevel, menuInfoQuery.getLevel());
        }
        if (menuInfoQuery.getSort() != null) {
            wrapper.eq(MenuInfo::getSort, menuInfoQuery.getSort());
        }
        if (!StringUtils.isEmpty(menuInfoQuery.getPath())) {
            wrapper.like(MenuInfo::getPath, menuInfoQuery.getPath());
        }
        if (menuInfoQuery.getIsOuterLink() != null) {
            wrapper.eq(MenuInfo::getIsOuterLink, menuInfoQuery.getIsOuterLink());
        }
        if (menuInfoQuery.getIsHidden() != null) {
            wrapper.eq(MenuInfo::getIsHidden, menuInfoQuery.getIsHidden());
        }
        if (!StringUtils.isEmpty(menuInfoQuery.getRemark())) {
            wrapper.like(MenuInfo::getRemark, menuInfoQuery.getRemark());
        }
        return wrapper;
    }

    /**
     * 按照条件查询菜单信息
     *
     * @param menuInfoQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<MenuInfo>> list(MenuInfoQuery menuInfoQuery) {
        return Result.success(menuInfoService.list(getMenuInfoQueryWrapper(menuInfoQuery)));
    }

    /**
     * 分页及条件查询菜单信息
     *
     * @param page          页码
     * @param size          分页大小
     * @param menuInfoQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<MenuInfo>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, MenuInfoQuery menuInfoQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<MenuInfo> pageParam = new Page<>(page, size);

        menuInfoService.page(pageParam, getMenuInfoQueryWrapper(menuInfoQuery));
        PageResult<MenuInfo> pageResult = new PageResult<MenuInfo>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 根据ID获取菜单详情
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @Override
    @GetMapping("/{id}")
    public Result<MenuInfo> detail(@PathVariable("id") String id) {
        return Result.success(menuInfoService.detailById(id));
    }

    /**
     * 增加菜单信息
     *
     * @param menuInfo 菜单增加实体
     * @return 增加后的菜单信息
     */
    @Override
    @PostMapping
    public Result<MenuInfo> add(@RequestBody @Validated(AddGroup.class) MenuInfo menuInfo) {
        menuInfoService.saveMenuInfo(menuInfo);
        return Result.success(menuInfo);
    }

    /**
     * 修改菜单信息
     *
     * @param id       菜单ID
     * @param menuInfo 菜单修改实体
     * @return 修改后的菜单信息
     */
    @Override
    @PutMapping("/{id}")
    public Result<MenuInfo> updateById(@PathVariable("id") String id, @RequestBody @Validated(UpdateGroup.class) MenuInfo menuInfo) {
        menuInfo.setId(id);
        menuInfoService.updateMenuInfoById(menuInfo);
        return Result.success(menuInfoService.getById(id));
    }


    /**
     * 删除菜单信息
     *
     * @param id 菜单ID
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        menuInfoService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 删除菜单信息
     *
     * @param idList 菜单ID数组
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        menuInfoService.removeByIds(idList);
        return Result.success(true);
    }

    /**
     * 查询菜单树形结构
     *
     * @return 菜单树形结构
     */
    @Override
    @GetMapping("/tree")
    public Result<List<MenuInfoTreeVo>> tree() {
        return Result.success(menuInfoService.tree());
    }

    /**
     * 根据登录用户信息获取菜单列表
     *
     * @return 此用户的菜单列表信息
     */
    @Override
    @GetMapping("/tree-auth")
    @RequiresLogin
    public Result<List<MenuInfoTreeVo>> treeMenuByLoginUser(LoginUser loginUser) {
        return null;
    }

    /**
     * 根据用户ID获取拥有的权限标识列表
     *
     * @param userId 用户ID
     * @return 菜单标识列表
     */
    @Override
    @GetMapping("/menu/keys/user/{userId}")
    public Result<Set<String>> getMenuKeysByUserId(@PathVariable("userId") String userId) {
        return Result.success(menuInfoService.getMenuKeysByUserId(userId));
    }

}
